<?php
include "../../Reportes/config.php";
include "../../Reportes/conexion.php";


$sql = "SELECT a.id_autor, ap.id_paper, id_pago, nombre, apellido, email_1, email_2, institucion, telefono, cargo, id_pais, p.id_resultado
FROM autor_paper ap
INNER JOIN autor a ON a.id_autor = ap.id_autor
LEFT JOIN paper p on p.id_paper = ap.id_paper
";

//$sql = "select * from track order by nombre";

//echo $sql;
//print_r($dbh);
//exit();

foreach($dbh->query($sql) as $row) {

	$idtipocategoria = "NULL";
	if($row["id_resultado"] == 1){
		$idtipocategoria = 3;
	}

	if($row["id_resultado"] == 2){
		$idtipocategoria = 4;
	}	
	
	//echo "123";
	$id_autor = $row["id_autor"];
	$id_paper = $row["id_paper"];
	$nombre = "";
	$apellido = "";
	$nombreCompleto = ""; 
	$email_1 = "";
	$email_2 = "";
	$institucion = "";
	$telefono = "";
	$cargo = "";
	$id_pais = "";
	$id_pago = "";
	
	
	$nombre = strtoupper($row["nombre"]);
	$apellido = strtoupper($row["apellido"]);
	$nombreCompleto = $nombre." ".$apellido; 
	$email_1 = $row["email_1"];
	$email_2 = $row["email_2"];
	$institucion = $row["institucion"];
	$telefono = $row["telefono"];
	$cargo = $row["cargo"];
	$id_pais = $row["id_pais"];
	$id_pago = $row["id_pago"];
		
	$sqlVerificar = "select * from invitado where id_autor = ".$id_autor." and id_paper=".$id_paper.""; 
	
	//echo $sqlVerificar."<br>";
	$existe = 0;
	foreach($dbh->query($sqlVerificar) as $rowInvitado) {		
		$existe = 1;
	}
	if($existe == 0){
		//inserta
		$sqlInsertar = "insert into invitado (id_autor, id_paper, Nombre, Paterno, Materno, NombreCompleto, Email_1, Email_2,
			Institucion, Telefono, Cargo, id_pais, id_pago, 	invitadoCategoria_id
		) values (
		'".$id_autor."',
		'".$id_paper."',
		'".$nombre."',
		'".$apellido."',
		'',
		'".$nombreCompleto."',
		'".$email_1."',
		'".$email_2."',
		'".$institucion."',
		'".$telefono."',
		'".$cargo."',
		'".$id_pais."',
		'".$id_pago."',
		".$idtipocategoria."
		)";
		
		$q = $dbh->prepare($sqlInsertar);
		$q->execute();
		
		echo $sqlInsertar."<br/>";
	}
	
	
	
}
?>